package architecture.reports;

import java.io.*;
import java.sql.*;
import java.util.*;

import org.xml.sax.*;

import db.*;

import sf.jsonloader.*;
import sf.view.fields.*;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.engine.xml.*;

import static architecture.reports.XmlReport.*;

public class Reporting {

	public static final int SIMPLE_REPORT_TYPE = 0;
	public static final int HIERARCHY_REPORT_TYPE = 1;
	public static final int LIST_REPORT_TYPE = 2;
	
	
	private static HashMap getColumn(ArrayList columns, String columnCode) {
		for (Object c : columns) {
			HashMap hc = (HashMap) c;
			if (hc.get("code").equals(columnCode)) {
				return hc;
			}
		}
		return null;
	}
	
	public static void createReport(String tableName, ebDBDataSet dataSet, String destination, int reportType) {
		try {
			System.out.println("----------------------------------");
			System.out.println(tableName);
			System.out.println("Creating report");
			ResultSet resultSet = dataSet.getResultSet();
			ResultSetMetaData metaData = resultSet.getMetaData();
			//System.out.println(resultSet.toString());
			//System.out.println(metaData.toString());
			
			JRDataSource dataSource = new JRResultSetDataSource(resultSet);// new
						
	
			int cc = metaData.getColumnCount();	
			
			String[] names = new String[cc] ;
			String[] labels = new String[cc];
			String[] types = new String[cc];
			int[] starts = new int[cc];
			int[] widths = new int[cc];
			
			JsonLoader json = JsonLoader.getInstance(); 
			
			for(int i = 0;i<cc;i++)
			{
				names[i] = metaData.getColumnName(i+1);
				labels[i] = (String) getColumn(
						(ArrayList) json.get(tableName, "columns"), metaData.getColumnName(i+1))
						.get("name"); ;
				types[i] = "java.lang.String";
				starts[i] = (i==0)?20:starts[i-1]+widths[i-1];
				widths[i] = dataSet.printableColumnSize(i+1 , labels[i].length());
				
				//System.out.println(names[i] + " " +labels[i] + " " +types[i] + " ");
			}
			
			int scale = Math.min(6, (pageWidth-40)/(starts[cc-1]+widths[cc-1]));
			
			StringBuilder jasperReport = new StringBuilder();
			
			setType(reportType);
			setSizes(cc, pageWidth-60, 30);
			jasperReport.append(header).append("\n");
			jasperReport.append(jasperStartTag((String)json.get(tableName,"name"))).append("\n");
			for (int i = 0;i<names.length;i++)
				jasperReport.append(field(names[i], types[i])).append("\n");
			
			jasperReport.append(sort(names[0])).append("\n");
			jasperReport.append(title((String)json.get(tableName,"name"))).append("\n");
			jasperReport.append(pageHeader(labels, starts, widths, scale)).append("\n");
			jasperReport.append(detail(names, types, starts, widths, scale)).append("\n");
			jasperReport.append(pageFooter()).append("\n");
			jasperReport.append(jasperEndTag).append("\n");
			
			//System.out.println("---------------------------------------------");
			
			//System.out.println(jasperReport.toString());
			InputStream reportInput = new StringBufferInputStream(jasperReport.toString());
			

			// design.set
			JasperReport report = JasperCompileManager
					.compileReport(reportInput);//design);//jasperReportFile);
			JasperPrint print = JasperFillManager.fillReport(report,
					new HashMap(), dataSource);
			JasperExportManager.exportReportToPdfFile(print, destination);
			//System.out.println("left = " + leftMargin + " intern = " + internWidth + " right = " +rightMargin );
			System.out.println("Report " + destination + " printed");
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}